/**
 * @ Author 何涛
 * @ Date 2016/02/29 07:02:35
 *//*

tableViewPro.Menu = cc.Menu.extend({
    parentNode: null,
    touchArea: null,
    isTouchLimit: false,

    centerPoint: null,
    endPoint: null,

    ctor: function (menuItems) {
        this._super(menuItems);

        var self = this;
        var listener1 = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: false,
            self: self,
            onTouchBegan: this._onTouchBegan1,
            onTouchMoved: this._onTouchMoved1,
            onTouchEnded: this._onTouchEnded1,
            onTouchCancelled: this._onTouchCancelled1
        });
        cc.eventManager.addListener(listener1, -1);
    },

    onEnter: function () {
        this._super();

        if (this._touchListener) {
            cc.eventManager.removeListener(this._touchListener);
        }

        this._touchListener = null;

        var listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: false,
            onTouchBegan: this._onTouchBegan,
            onTouchMoved: this._onTouchMoved,
            onTouchEnded: this._onTouchEnded,
            onTouchCancelled: this._onTouchCancelled
        });
        this._touchListener = listener;

        cc.eventManager.addListener(listener, this);
    },

    setTouchArea: function (size, position) {
        this.touchArea = cc.rect(position.x, position.y, size.width, size.height);
        this.isTouchLimit = true;

//TODO 测试按钮触摸区域
	var dn = new cc.DrawNode();
	dn.drawRect(cc.p(position.x, position.y), cc.p(position.x + size.width, position.y + size.height), cc.color(0, 0, 0, 70), 1, cc.color(0, 0, 0, 70));
	var rs = cc.director.getRunningScene();
	rs.addChild(dn, 100);
	rs.scheduleOnce(function () {
	    dn.removeFromParent(true);
	}, 1);
    },

    setMyParent: function (node) {
        this.parentNode = node;
    },

    isInTouchArea: function (point) {
        if (this.isTouchLimit) {
            if (cc.rectContainsPoint(this.touchArea, point)) {
                return true;
            }
        }
        return false;
    },

    _onTouchBegan: function (touch, event) {
        var target = event.getCurrentTarget();
        var point = touch.getLocation();

        if (!target.isInTouchArea(point))
            return false;

        this.centerPoint = point;

        if (target._state != cc.MENU_STATE_WAITING || !target._visible || !target.enabled)
            return false;

        for (var c = target.parent; c != null; c = c.parent) {
            if (!c.isVisible())
                return false;
        }

        target._selectedItem = target._itemForTouch(touch);
        if (target._selectedItem) {
            target._state = cc.MENU_STATE_TRACKING_TOUCH;
            target._selectedItem.selected();
            target._selectedItem.setNodeDirty();
            return true;
        }
        return false;
    },

    _onTouchMoved: function (touch, event) {
        var target = event.getCurrentTarget();
        if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
            cc.log("cc.Menu.onTouchMoved(): invalid state");
            return;
        }
        var currentItem = target._itemForTouch(touch);
        if (currentItem != target._selectedItem) {
            if (target._selectedItem) {
                target._selectedItem.unselected();
                target._selectedItem.setNodeDirty();
            }
            target._selectedItem = currentItem;
            if (target._selectedItem) {
                target._selectedItem.selected();
                target._selectedItem.setNodeDirty();
            }
        }
    },

    _onTouchEnded: function (touch, event) {
        var target = event.getCurrentTarget();
        this.endPoint = touch.getLocation();

        if (Math.abs(this.endPoint.y - this.centerPoint.y) > 5) {
            if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
                cc.log("cc.Menu.onTouchCancelled(): invalid state");
                return;
            }

            if (this._selectedItem) {
                target._selectedItem.unselected();
                target._selectedItem.setNodeDirty();
            }
            target._state = cc.MENU_STATE_WAITING;

            return;
        }

        if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
            cc.log("cc.Menu.onTouchEnded(): invalid state");
            return;
        }
        if (target._selectedItem) {
            target._selectedItem.unselected();
            target._selectedItem.setNodeDirty();
            target._selectedItem.activate();
        }
        target._state = cc.MENU_STATE_WAITING;
    },

    _onTouchCancelled: function (touch, event) {
        var target = event.getCurrentTarget();
        if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
            cc.log("cc.Menu.onTouchCancelled(): invalid state");
            return;
        }
        if (this._selectedItem) {
            target._selectedItem.unselected();
            target._selectedItem.setNodeDirty();
        }
        target._state = cc.MENU_STATE_WAITING;
    },

    _onTouchBegan1: function (touch, event) {
        var target = this.self;

        var point = touch.getLocation();

        if (!target.isInTouchArea(point))
            return false;

        return true;
    },

    _onTouchMoved1: function (touch, event) {
        var self = this.self;
        var target = self.parentNode;

        target.onTouchMoved1.call(null, touch, target);
    },

    _onTouchEnded1: function (touch, event) {

    },

    _onTouchCancelled1: function (touch, event) {

    }
});*/
//dzmjNamespace.TyMenu = cc.Menu.extend(/** @lends cc.Menu# */{
//touchArea:null,
//isTouchLimit:false,
//_touchListener: null,

//ctor: function (menuItems) {
//this._super(menuItems);
////cc.Menu.prototype.ctor.apply(this, menuItems);

//this._touchListener = cc.EventListener.create({
//event: cc.EventListener.TOUCH_ONE_BY_ONE,
//swallowTouches: false,
//onTouchBegan: this.onTouchBegan,
//onTouchMoved: this.onTouchMoved,
//onTouchEnded: this.onTouchEnded,
//onTouchCancelled: this.onTouchCancelled,

//onTouchBegan:function (touch, event) {
//var target = event.getCurrentTarget();

////cc.log("是否在触摸限制范围内:" + target.isInTouchArea(touch, event));

//cc.log("target._state:" + target._state);
//cc.log("!target._visible:" + !target._visible);
//cc.log("!target.enabled:" + !target.enabled);

//if (target._state !== cc.MENU_STATE_WAITING || !target._visible || !target.enabled){
//return false;
//}

//for (var c = target.parent; c != null; c = c.parent) {
//if (!c.isVisible())
//return false;
//}

//cc.log("target.isInTouchArea(touch, event):" + target.isInTouchArea(touch, event));

//if(!target.isInTouchArea(touch, event)){
//return false;
//}

//target._selectedItem = target._itemForTouch(touch);
//if (target._selectedItem) {
//target._state = cc.MENU_STATE_TRACKING_TOUCH;
//target._selectedItem.selected();
//// target._selectedItem.setNodeDirty();
//return true;
//}

//return false;
//},

//onTouchEnded: function (touch, event) {
//var target = event.getCurrentTarget();
//if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
//cc.log("cc.Menu.onTouchEnded(): invalid state");
//return;
//}
//if (target._selectedItem) {
//target._selectedItem.unselected();
////target._selectedItem.setNodeDirty();
//target._selectedItem.activate();
//}
//target._state = cc.MENU_STATE_WAITING;
//},

//onTouchCancelled: function (touch, event) {
//var target = event.getCurrentTarget();
//if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
//cc.log("cc.Menu.onTouchCancelled(): invalid state");
//return;
//}
//if (this._selectedItem) {
//target._selectedItem.unselected();
////target._selectedItem.setNodeDirty();
//}
//target._state = cc.MENU_STATE_WAITING;
//},

//onTouchMoved: function (touch, event) {
//var target = event.getCurrentTarget();
//if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
//cc.log("cc.Menu.onTouchMoved(): invalid state");
//return;
//}
//var currentItem = target._itemForTouch(touch);
//if (currentItem !== target._selectedItem) {
//if (target._selectedItem) {
//target._selectedItem.unselected();
////target._selectedItem.setNodeDirty();
//}
//target._selectedItem = currentItem;
//if (target._selectedItem) {
//target._selectedItem.selected();
////target._selectedItem.setNodeDirty();
//}
//}
//}
//});

////cc.Menu.prototype.enabled.swallowTouches = false;

//cc.log("this._touchListener :" +  this._touchListener);

////this._touchListener.swallowTouches = false;

////cc.log("cc.Menu.prototype.enabled :" +  cc.Menu.prototype.enabled);
//},

//setTouchArea:function (touchArea) {
//this.touchArea = touchArea;
//this.isTouchLimit = true;
////this.touchArea.retain();
//},

//isInTouchArea:function (touch, event) {
//var target = event.getCurrentTarget();
//if(target && target.isTouchLimit){
//var locationInNode = target.touchArea.convertToNodeSpace(touch.getLocation());
//var s = target.touchArea.getContentSize();
//var rect = cc.rect(0, 0, s.width, s.height);
//if (cc.rectContainsPoint(rect, locationInNode)) {
//return true;
//}
//}
//return false;
//},
//});


tableViewPro.Menu = cc.Layer.extend(/** @lends cc.Menu# */{
	enabled: false,
	touchArea: null,
	isTouchLimit: false,
	_visible: true,

	_selectedItem: null,
	_state: -1,
	_touchListener: null,
	_className: "TyMenu",

	ctor: function (menuItems) {
		this._super();
		//cc.Layer.prototype.ctor.call(this);
		this._color = cc.color.WHITE;
		this.enabled = false;
		this._opacity = 255;
		this._selectedItem = null;
		this._state = -1;

		//this._touchListener = cc.EventListener.create({
		//	event: cc.EventListener.TOUCH_ONE_BY_ONE,
		//	swallowTouches: false,
		//	onTouchBegan: this._onTouchBegan,
		//	onTouchMoved: this._onTouchMoved,
		//	onTouchEnded: this._onTouchEnded,
		//	onTouchCancelled: this._onTouchCancelled
		//});

		if ((arguments.length > 0) && (arguments[arguments.length - 1] == null))
			cc.log("parameters should not be ending with null in Javascript");

		var argc = arguments.length, items;
		if (argc === 0) {
			items = [];
		} else if (argc === 1) {
			if (menuItems instanceof Array) {
				items = menuItems;
			}
			else items = [menuItems];
		}
		else if (argc > 1) {
			items = [];
			for (var i = 0; i < argc; i++) {
				if (arguments[i])
					items.push(arguments[i]);
			}
		}
		this.initWithArray(items);
	},

	onEnter: function () {
		cc.Node.prototype.onEnter.call(this);
	},

	isEnabled: function () {
		return this.enabled;
	},

	setEnabled: function (enabled) {
		this.enabled = enabled;
	},

	initWithItems: function (args) {
		var pArray = [];
		if (args) {
			for (var i = 0; i < args.length; i++) {
				if (args[i])
					pArray.push(args[i]);
			}
		}

		return this.initWithArray(pArray);
	},

	initWithArray: function (arrayOfItems) {
		if (cc.Layer.prototype.init.call(this)) {
			this.enabled = true;

			// menu in the center of the screen
			var winSize = cc.winSize;
			this.setPosition(winSize.width / 2, winSize.height / 2);
			this.setContentSize(winSize);
			this.setAnchorPoint(0.5, 0.5);
			this.ignoreAnchorPointForPosition(true);

			if (arrayOfItems) {
				for (var i = 0; i < arrayOfItems.length; i++)
					this.addChild(arrayOfItems[i], i);
			}

			this._selectedItem = null;
			this._state = cc.MENU_STATE_WAITING;

			// enable cascade color and opacity on menus
			this.cascadeColor = true;
			this.cascadeOpacity = true;

			return true;
		}
		return false;
	},

	setTouchArea: function (size, position) {
//		this.touchArea = touchArea;
//		this.isTouchLimit = true;
		this.touchArea = cc.rect(position.x, position.y, size.width, size.height);
		this.isTouchLimit = true;
	},
	
	setMyParent: function (node) {
		this.parentNode = node;
	},
	
	setTouchListener: function () {
		this._touchListener = cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: false,
			onTouchBegan: this._onTouchBegan,
			onTouchMoved: this._onTouchMoved,
			onTouchEnded: this._onTouchEnded,
			onTouchCancelled: this._onTouchCancelled
		});

		var locListener = this._touchListener;
		if (!locListener._registered)
			cc.eventManager.addListener(locListener, this);
	},

	isInTouchArea: function (point) {
		/*var target = event.getCurrentTarget();
		if (target && target.isTouchLimit) {
			var locationInNode = target.touchArea.convertToNodeSpace(touch.getLocation());
			var s = target.touchArea.getContentSize();
			var rect = cc.rect(0, 0, s.width, s.height);
			if (cc.rectContainsPoint(rect, locationInNode)) {
				return true;
			}
		}
		return false;*/

		if (this.isTouchLimit) {
			if (cc.rectContainsPoint(this.touchArea, point)) {
				return true;
			}
		}
		return false;
	},
	
	removeChild: function (child, cleanup) {
		if (child == null)
			return;
		if (!(child instanceof cc.MenuItem)) {
			cc.log("cc.Menu.removeChild():Menu only supports MenuItem objects as children");
			return;
		}

		if (this._selectedItem === child)
			this._selectedItem = null;
		cc.Node.prototype.removeChild.call(this, child, cleanup);
	},

	_onTouchBegan: function (touch, event) {
		var target = event.getCurrentTarget();
		var point = touch.getLocation();

//		cc.log("是否在触摸限制范围内:" + target.isInTouchArea(touch, event));

//		cc.log("target._state:" + target._state);
//		cc.log("!target._visible:" + !target._visible);
//		cc.log("!target.enabled:" + !target.enabled);

		if (target._state !== cc.MENU_STATE_WAITING || !target._visible || !target.enabled) {
			return false;
		}

		for (var c = target.parent; c != null; c = c.parent) {
			if (!c.isVisible())
				return false;
		}

		if (!target.isInTouchArea(point)) {
//			if (!target.isInTouchArea(touch, event)) {
			return false;
		} 

		target._selectedItem = target._itemForTouch(touch);
		if (target._selectedItem) {
			target._state = cc.MENU_STATE_TRACKING_TOUCH;
			target._selectedItem.selected();
			// target._selectedItem.setNodeDirty();
			return true;
		}

		return false;
	},

	_onTouchEnded: function (touch, event) {
		var target = event.getCurrentTarget();
		if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
			cc.log("cc.Menu.onTouchEnded(): invalid state");
			return;
		}
		if (target._selectedItem) {
			target._selectedItem.unselected();
//			target._selectedItem.setNodeDirty();
			target._selectedItem.activate();
		}
		target._state = cc.MENU_STATE_WAITING;
	},

	_onTouchCancelled: function (touch, event) {
		var target = event.getCurrentTarget();
		if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
			cc.log("cc.Menu.onTouchCancelled(): invalid state");
			return;
		}
		if (this._selectedItem) {
			target._selectedItem.unselected();
//			target._selectedItem.setNodeDirty();
		}
		target._state = cc.MENU_STATE_WAITING;
	},

	_onTouchMoved: function (touch, event) {
		var target = event.getCurrentTarget();
		if (target._state !== cc.MENU_STATE_TRACKING_TOUCH) {
			cc.log("cc.Menu.onTouchMoved(): invalid state");
			return;
		}
		var currentItem = target._itemForTouch(touch);
		if (currentItem !== target._selectedItem) {
			if (target._selectedItem) {
				target._selectedItem.unselected();
//				target._selectedItem.setNodeDirty();
			}
			target._selectedItem = currentItem;
			if (target._selectedItem) {
				target._selectedItem.selected();
//				target._selectedItem.setNodeDirty();
			}
		}
	},

	onExit: function () {
		if (this._state === cc.MENU_STATE_TRACKING_TOUCH) {
			if (this._selectedItem) {
				this._selectedItem.unselected();
				this._selectedItem = null;
			}
			this._state = cc.MENU_STATE_WAITING;
		}
		cc.Node.prototype.onExit.call(this);
	},

	setOpacityModifyRGB: function (value) {
	},

	isOpacityModifyRGB: function () {
		return false;
	},

	_itemForTouch: function (touch) {
		var touchLocation = touch.getLocation();
		var itemChildren = this.getChildren();
		if (itemChildren && itemChildren.length > 0) {
			for (var i = itemChildren.length - 1; i >= 0; i--) {
				locItemChild = itemChildren[i];
				if (locItemChild.isVisible() && locItemChild.isEnabled()) {
					var local = locItemChild.convertToNodeSpace(touchLocation);
					var r = locItemChild.rect();
					r.x = 0;
					r.y = 0;
					if (cc.rectContainsPoint(r, local))
						return locItemChild;
				}
			}
		}
		return null;
	
	}
	
	
});

var _p = tableViewPro.Menu.prototype;

//Extended properties
/** @expose */
_p.enabled;

tableViewPro.Menu.create = function (menuItems) {
	var argc = arguments.length;
	if ((argc > 0) && (arguments[argc - 1] == null))
		cc.log("parameters should not be ending with null in Javascript");

	var ret;
	if (argc === 0)
		ret = new cc.Menu();
	else if (argc === 1)
		ret = new cc.Menu(menuItems);
	else
		ret = new cc.Menu(Array.prototype.slice.call(arguments, 0));
	return ret;
};